/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.AlloBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AlloBillDao;
import com.minierp.dao.AlloBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.ProductDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.err.NotAllowException;
import com.minierp.err.StockNoEnoughException;
import com.minierp.helper.EncAlloBillFormCtrHelper;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.AlloBill;
import com.minierp.model.CodeRule;
import com.minierp.model.TallySet;
import com.minierp.service.AlloBillService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class AlloBillFormCtr extends SimpleFormController {
	private AlloBillItemDao alloBillItemDao;
	private AlloBillDao alloBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private StorehouseDao storehouseDao;
	private ProductDao productDao;
	private StockDao stockDao;
	private StockBalanceDao stockBalanceDao;
	private IdGenerator idGenerator;
	private AlloBillService alloBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		List stoList;
		if (id != null && !id.equals("")) {
			AlloBill ub1 = (AlloBill) this.alloBillDao.get(AlloBill.class,
					Long.parseLong(id));
			stoList = this.storehouseDao.getList(false);
			refData.put(
					"fromStoList",
					PageHelper.parseStorList(stoList, ub1.getTallySetId(),
							ub1.getFromStoId()));
			refData.put(
					"toStoList",
					PageHelper.parseStorList(stoList, ub1.getTallySetId(),
							ub1.getToStoId()));
		} else {
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			stoList = this.storehouseDao.getList(ub.getUser().getDfTsId());
			refData.put("fromStoList", stoList);
			refData.put("toStoList", stoList);
		}

		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("ALLO_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for AlloBill");
		} else {
			AlloBillBean form;
			if (id != null && !id.equals("")) {
				AlloBill alloBill = (AlloBill) this.alloBillDao.get(
						AlloBill.class, Long.parseLong(id));
				System.out.println("*********************exam:"
						+ alloBill.getExamined());
				if (alloBill == null) {
					throw new Exception("Record not found for id:" + id);
				}

				TallySet ts = (TallySet) this.alloBillDao.get(TallySet.class,
						alloBill.getTallySetId());
				List itemList = this.alloBillItemDao.getListWidthProd(alloBill
						.getId());
				Object stocList = new ArrayList();
				if (alloBill.getExamined().equals("N")) {
					stocList = this.alloBillItemDao.getStockList(
							alloBill.getId(), alloBill.getFromStoId());
				}

				form = EncAlloBillFormCtrHelper.doFormBackingObjectEdit(cr,
						alloBill, ts, itemList, (List) stocList);
			} else {
				form = EncAlloBillFormCtrHelper.doFormBackingObjectNew(request,
						cr);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		AlloBillBean form = (AlloBillBean) command;
		EncAlloBillFormCtrHelper.doOnBind(request, form, this.dataSource);
	}

	protected void onBindAndValidate(HttpServletRequest request,
			Object command, BindException errors) throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		String alertMsg = "";
		AlloBillBean form = (AlloBillBean) command;
		AlloBill obj = form.getAlloBill();
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "ALLO_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			obj.setExamineTime(new Date());
			obj.setExaminerId(ub.getStaffer().getId());
			obj.setExaminerName(ub.getStaffer().getStafferName());
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "ALLO_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.alloBillService.txUpdate(form, this.alloBillDao,
						this.alloBillItemDao, this.stockDao,
						this.stockBalanceDao, this.idGenerator);
			} catch (StockNoEnoughException arg13) {
				EncCommonHelper.setAlertMessage(request, arg13.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg14) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg15) {
				EncCommonHelper.setAlertMessage(request, arg15.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "修改成功！";
		} else {
			try {
				this.alloBillService.txInsert(form, this.alloBillDao,
						this.alloBillItemDao, this.stockDao,
						this.stockBalanceDao, this.serialNumberDao,
						this.idGenerator);
			} catch (StockNoEnoughException arg16) {
				EncCommonHelper.setAlertMessage(request, arg16.getMessage());
				return this.showForm(request, errors, this.getFormView());
			} catch (CannotAcquireLockException arg17) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg18) {
				EncCommonHelper.setAlertMessage(request, arg18.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}

			alertMsg = "增加成功！";
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		String continueFlag = request.getParameter("continue_flag");
		String alertType = request.getParameter("alert_type");
		if (continueFlag != null && continueFlag.equals("1")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			mv = new ModelAndView(
					"redirect:/storehouse/AlloBillNew.ihtm?alert_type="
							+ alertType);
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, alertMsg);
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/storehouse/AlloBillEdit.ihtm?id=" + obj.getId()
								+ "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public AlloBillDao getAlloBillDao() {
		return this.alloBillDao;
	}

	public void setAlloBillDao(AlloBillDao alloBillDao) {
		this.alloBillDao = alloBillDao;
	}

	public AlloBillItemDao getAlloBillItemDao() {
		return this.alloBillItemDao;
	}

	public void setAlloBillItemDao(AlloBillItemDao alloBillItemDao) {
		this.alloBillItemDao = alloBillItemDao;
	}

	public AlloBillService getAlloBillService() {
		return this.alloBillService;
	}

	public void setAlloBillService(AlloBillService alloBillService) {
		this.alloBillService = alloBillService;
	}

	public StockBalanceDao getStockBalanceDao() {
		return this.stockBalanceDao;
	}

	public void setStockBalanceDao(StockBalanceDao stockBalanceDao) {
		this.stockBalanceDao = stockBalanceDao;
	}
}